﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly ISqlSugarClient _db;

        public StudentController(SqlSugar.ISqlSugarClient db)
        {
            this._db = db;
        }

        /// <summary>
        /// 查询学生信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStudentAll(int pageindex,int pagesize)
        {
            var list = _db.Queryable<StudentModel>().ToPageList(pageindex, pagesize).ToList();// 不返回Count;
            return Ok(list);
        }

        /// <summary>
        /// 添加学生信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateStudent(StudentModel s)
        {
            var r= _db.Insertable(s).ExecuteReturnIdentity();
            return Ok(r);
        }

        /// <summary>
        /// 删除学生信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult DeleteStudent(int id)
        {
            //单个实体
            var r = _db.Deleteable<StudentModel>(new StudentModel() { Id = id }).ExecuteCommand();

            return Ok(r);
        }

        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateStudent(StudentModel s)
        {
            //根据主键更新单条 参数 Class
            var r = _db.Updateable(s).ExecuteCommand();//实体有多少列更新多少列

            return Ok(r);
        }
    }
}

